{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基数排序\n",
    "## 所有待排序元素均为整数，至多为 d 位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from Counting_sort import *\n",
    "import math\n",
    "\n",
    "def Radix_sort(L, d=256):\n",
    "    for i in range(d):\n",
    "        bucket = [[] for i in range(10)] # 十进制 所以用 十个桶进行排序\n",
    "        for j in L:\n",
    "            bucket[j // (10 ** i) % 10].append(j)\n",
    "        L = [a for b in bucket for a in b]\n",
    "    return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 6, 7, 26, 48, 48]\n"
     ]
    }
   ],
   "source": [
    "# running this to check radix_sort\n",
    "L = [4,5,48,6,26,48,7]\n",
    "print(Radix_sort(L,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
